<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>申购详情</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div class="apply-box" v-if="detailOption">
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                名称:
              </div>
              <div class="apply-box-list-l-content apply-box-list-l-content-img">
                <ImagePreview :src="detailOption.pic" width="40px" height="40px"></ImagePreview>
                <p>{{ getName(detailOption.name) }}</p>
              </div>
            </div>
          </div>
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                申购数量:
              </div>
              <div class="apply-box-list-l-content">
                {{ getName(detailOption.need_number) }}
              </div>
            </div>
            <div class="apply-box-list-r">
              <div class="apply-box-list-l-name">
                单位:
              </div>
              <div class="apply-box-list-l-content">
                {{ getName(detailOption.unit) }}
              </div>
            </div>
          </div>
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                单价:
              </div>
              <div class="apply-box-list-l-content">
                {{ getName(detailOption.price) }}
              </div>
            </div>
            <div class="apply-box-list-r">
              <div class="apply-box-list-l-name">
                申购事由:
              </div>
              <div class="apply-box-list-l-content">
                {{ getName(detailOption.reason) }}
              </div>
            </div>
          </div>
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                链接/单位:
              </div>
              <div class="apply-box-list-l-content url-link">
                <div v-clipboard:copy="detailOption.url" v-clipboard:success="onCopy" v-clipboard:error="onError">
                  <div class="url-link-text">{{ getName(detailOption.url) }}</div>
                  <i class="el-icon-document-copy"></i>
                </div>
              </div>
            </div>
            <div class="apply-box-list-r">
              <div class="apply-box-list-l-name">
                期望时间:
              </div>
              <div class="apply-box-list-l-content">
                {{ getName(detailOption.expect_time) }}
              </div>
            </div>
          </div>
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                实际下单数:
              </div>
              <div class="apply-box-list-l-content">
                {{ detailOption.actual_number }}
              </div>
            </div>
            <div class="apply-box-list-r" v-if="pageType=='examine'">
              <div class="apply-box-list-l-name">
                申购状态
              </div>
              <div class="apply-box-list-l-content">
                <!--                // 0待审核 1通过 2驳回 3未入库 4已入库-->
                <!--                scope.row.pay_time 下单时间-->
                <!--                scope.row.actual_time 入库时间 underline-->
                <el-link :underline="false" slot="reference" v-if="!detailOption.examine_user_status" type="primary">
                  待审批
                </el-link>
                <el-link :underline="false" v-else slot="reference"
                         :type="detailOption.examine_user_status==2?'danger':'success'">
                  {{ detailOption.examine_user_status == 2 ? '已驳回' : '已通过' }}
                </el-link>
              </div>
            </div>
            <div class="apply-box-list-r" v-if="pageType=='search'">
              <div class="apply-box-list-l-name">
                申购状态
              </div>
              <div class="apply-box-list-l-content">
                <!--                // 0待审核 1通过 2驳回 3未入库 4已入库-->
                <!--                scope.row.pay_time 下单时间-->
                <!--                scope.row.actual_time 入库时间 underline-->

                <el-link :underline="false" v-if="detailOption.status == 0" type="warning">待审批</el-link>
                <el-link :underline="false" v-if="detailOption.status == 2" type="danger">已驳回</el-link>
                <el-link type="warning" :underline="false"
                         v-if="detailOption.status == 1 && detailOption.pay_time == '1970-01-01'">待下单
                </el-link>
                <el-link type="warning" :underline="false"
                         v-if="detailOption.status == 3">
                  待入库
                </el-link>
                <el-link type="warning" :underline="false"
                         v-if="detailOption.status == 4">
                  已入库
                </el-link>
              </div>
            </div>
          </div>
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                补充说明:
              </div>
              <div class="apply-box-list-l-content">
                {{ detailOption.remark }}
              </div>
            </div>
            <div class="apply-box-list-r">
              <div class="apply-box-list-l-name">
                资金来源:
              </div>
              <div class="apply-box-list-l-content">
                {{ detailOption.finance_source}}
              </div>
            </div>
          </div>
          <div class="apply-box-list">
            <div class="apply-box-list-l">
              <div class="apply-box-list-l-name">
                申购进程:
              </div>
              <div class="apply-box-list-l-content">
                <el-timeline>
                  <el-timeline-item
                      v-for="(activity, index) in detailOption.examine"
                      :key="index"
                      :color="activity.status==1?'#0bbd87':(activity.status==2?'red':'')"
                      :timestamp="activity.examineuser?activity.update_time:''">
                    <span>{{ activity.flow_name }}</span>
                    <span v-if="activity.examineuser">{{
                        activity.examineuser ? ',审批人:' + activity.examineuser.realname : ''
                      }}</span>
                    <span v-else>{{activity.user_text}}【{{ activity.status_text || '' }}】</span>
                    <span
                        v-if="activity.status">{{
                        activity.status == 1 ? ',通过审批' : ',驳回审批，驳回说明:' + activity.content
                      }}</span>
                  </el-timeline-item>
                  <el-timeline-item
                      v-if="detailOption.pay_time"
                      :color="detailOption.pay_time!=='1970-01-01'?'#0bbd87':''"
                      :timestamp="detailOption.pay_time!=='1970-01-01'?detailOption.pay_time:''">
                    <span>下单时间</span>
                  </el-timeline-item>
                  <el-timeline-item
                      v-if="detailOption.actual_time"
                      :color="detailOption.actual_time!=='1970-01-01'?'#0bbd87':''"
                      :timestamp="detailOption.actual_time!=='1970-01-01'?detailOption.actual_time:''">
                    <span>入库时间</span>
                  </el-timeline-item>
                </el-timeline>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="cornerComment-in-footer">
        <!--        <el-button @click.native="closePOpup" size="small">取消</el-button>-->
        <el-button size="small" @click="closePOpup">返回</el-button>
        <el-button type="primary" size="small" @click="examinBtn(1)" plain
                   v-if="pageType=='examine' && detailOption && !detailOption.examine_user_status && canExamin">
          通过
        </el-button>
        <el-button size="small" @click="examinBtn(2)" class="ml-10" plain type="danger"
                   v-if="pageType=='examine' && detailOption && !detailOption.examine_user_status && canExamin">驳回
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {purchaseDetailInfo} from '@/api/admin/purchase'
import {mapGetters} from "vuex";

export default {
  props: ['visible', 'detailId', 'pageType'], // pageType examine 单人审核查看 search 查询订单总览
  components: {},
  data() {
    return {
      dialogVisible: false,
      detailOption: null,
      canExamin: false
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.getDetailFn()
      } else {
        this.canExamin = false
        this.detailOption = null
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {

  },
  methods: {
    getDetailFn() {
      // pageType examine 单人审核查看 search 查询订单总览
      // examine 需要验证时否需要审核
      // search 全局申购流程
      purchaseDetailInfo({id: this.detailId}).then(res => {
        console.log('res:', res)
        // if(this.pageType==='examine'){
        // 判断该用户的状态
        let status_apply = 0
        let examinStatus = true // 如果上一级有驳回了就无法进行
        for (let items of res.vo.examine) {
          let data_ = items.user_ids.split(',').map(Number)
          if (data_.indexOf(this.userInfo.id) !== -1 && items.examine_user) {
            res.vo.examine_user_time = items.update_time
            res.vo.examine_user_status = items.status
          }
          if (!examinStatus) {
            res.vo.examine_user_status = 2
          }
          if (!status_apply) {
            if (items.status === 0) {
              items.status_text = '待审批'
            }
            if (items.status === 1) {
              items.status_text = '已审批'
            }
            if (items.status === 2) {
              items.status_text = '已驳回'
              status_apply = 1
            }
          } else {
            items.status_text = '终止'
          }
          if (items.status == 2) {
            status_apply = 1
            examinStatus = false
          }
          let user_ = []
          for(let user_item of items.flow_user){
            user_.push(user_item.realname)
          }
          items.user_text = user_.join(',')
        }
        // }
        this.canExamin = examinStatus
        this.detailOption = this.getJson(res.vo);
        console.log('页面数据"：', this.detailOption)
      })
    },
    examinBtn(type) {
      // type 1 单条通过 2 单条驳回 3 批量通过 4 批量驳回
      let that = this;
      if (type === 1) {
        this.$confirm('确认通过申购审批, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.subimitExamin(type)
        }).catch(() => {

        });
      } else if (type == 2) {
        this.$prompt('输入申购审批驳回说明', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          // value
          if (value) {
            that.subimitExamin(type, value)
          } else {
            this.$message.error('填写驳回理由')
          }

        }).catch(() => {

        });
      }
    },
    subimitExamin(type, value) {
      // option id 需要审核的ID type 类型 1 通过 2 驳回 content 理由
      // type 1 单条通过 2 单条驳回 3 批量通过 4 批量驳回
      let option = {id: this.detailId, type: type}
      if (type === 1) {
        this.putApplyReason(option)
      } else if (type === 2) {
        option.content = value;
        this.putApplyReason(option)
      }
    },
    putApplyReason(option) {
      console.log('option:', option)
      // type 1 单条通过 2 单条驳回 3 批量通过 4 批量驳回
      let n = this;
      this.$post(this.api.purchase.examineBtn, option).then((response) => {
        if (!response.code) {
          n.$message.success(response.msg)
          n.getDetailFn()
          n.$emit('InitFn')
        } else {
          n.$message.error(response.msg)
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    checkEditFn() {
      // if (!this.infoForm[this.itemlable]) {
      //   this.$message.error('请填写信息');
      //   return
      // }
      // this.$emit('checkList', {name: this.itemlable, val: this.infoForm[this.itemlable]})
      this.closePOpup()
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getName(option) {
      return option || '--'
    },
    // 复制成功
    onCopy() {
      this.$message.success('采购网址复制成功')
    },
    // 复制失败
    onError() {
      this.$message.error('采购网址复制失败')
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 800px;
  height: 700px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.jobTime span {
  padding: 0 10px;
}

.apply-box {
  border: 1px solid #aaa;
  font-size: 14px;
}

.apply-box-list {
  display: flex;
  border-bottom: 1px solid #aaa;
}

.apply-box-list:last-child {
  border-bottom: none;
}

.apply-box-list-l {
  flex: 1;
  display: flex;
}

.apply-box-list-l-name {
  width: 100px;
  padding: 10px;
  border-right: 1px solid #aaa;
  /*text-align: right;*/
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.apply-box-list-l-content {
  padding: 10px;
  flex: 1;
  word-wrap: break-word;
  width: 100px;
}

.apply-box-list-r {
  flex: 1;
  display: flex;
  /*padding:10px;*/
  border-left: 1px solid #aaa;
}

.apply-box-list-l-content-img {
  display: flex;
  align-items: center;
}

.apply-box-list-l-content-img img {
  width: 80px;
  height: 80px;
  margin-right: 10px;
}

.url-link {
  color: #3e89ff;
}

.url-link:hover {
  text-decoration: underline;
  cursor: pointer;
}

.url-link-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
